/**
* TG_Coder: Dzlua
* Email: 505544956@qq.com
* Time: 2017/05/10
*/
#pragma once

#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <Windows.h>
#include <jsoncpp/value.h>
#include <jsoncpp/reader.h>
#include <jsoncpp/writer.h>

namespace Common {

std::string lower(const std::string& str);

bool delete_file(const std::wstring & wstr_file_path);

bool ReadTxtFileA(std::string strFilePath, std::string& m_text);
bool ReadTxtFileW(std::string strFilePath, std::wstring& m_text);
bool LoadFromMem(BYTE* pByte, DWORD dwSize, std::wstring& m_text);

bool WriteTxtFileA(std::string m_filename, std::string m_text, int encoding = 2);
bool WriteTxtFileW(std::string m_filename, std::wstring m_text, int encoding = 2);

struct Str_Util{
    static int Replace_All(std::string& str, const std::string pattern, const std::string newpat);
	static int Replace_All(std::wstring& str, const std::wstring pattern, const std::wstring newpat);
    static int Splite_String2(std::string str, const std::string splite_str_sign, std::vector<std::string>& str_vector);
};

struct Char_Encode{
    enum encode_t
    {
        utf8 = CP_UTF8,
        win_sys = CP_ACP
    };
};

struct Str_Conv{

    //模板函数：将string类型变量转换为常用的数值类型（此方法具有普遍适用性）  
    template <class Type>  
    static Type StrToNum(const std::string& str)  
    {  
        std::istringstream iss(str);  
        Type num;  
        iss >> num;  
        return num;      
    }

    static std::string trim(std::string &s);
    static std::wstring trimW(std::wstring &s);
    static std::wstring	str2wstr(const std::string from, Char_Encode::encode_t char_code = Char_Encode::win_sys);
    static std::string	wstr2str(const std::wstring from, Char_Encode::encode_t char_code = Char_Encode::win_sys);
    static std::string GBK2UTF8(std::string strGBK);
};

struct Arr_Util{
    static bool Vector_Del(std::vector<std::string>& m_arr, std::string m_key);
    static bool Map_IsExistsA(std::map<std::string, std::string>& m_arr, std::string m_key);
    static bool Map_DelA(std::map<std::string, std::string>& m_arr, std::string m_key);
    static bool Map_GetA(std::map<std::string, std::string>& m_arr, std::string m_key, std::string& m_value);
};

struct Base64{
    static int pos(const char c);
    static unsigned int token_decode(const char *token);
    static int base64_encode(const void *data, const int size, char **str);
    static int base64_decode(const char *str, char *data);

    static int Encode(const std::string &szInput, std::string &szOutput);
    static int Decode(const std::string &szInput, std::string &szOutput);
};

} // end namespace Common